/**
 * 
 */
package com.architecture.util.jndi;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.architecture.util.object.ObjectUtility;
import com.architecture.util.string.StringUtility;

/**
 * <p>This class has utilities methods for be used to look up objects using <code>JNDI Framework</code>.</p>
 * @author JR
 * @version 1.0
 * @category JNDI Utilities
 */
public final class JndiUtility {
	
	/**
	 * private constructor of <code>JndiUtility</code>.
	 */
	private JndiUtility() {
		super();
	}
	
	/**
	 * <p>Look up object using JDNI tactic.</p>
	 * @param jndiName
	 * @param clazz
	 * @return <code>T</code>
	 */
	public static <T> T lookup(final String jndiName, final Class<T> clazz){
		try {
			if(ObjectUtility.isAllRef(jndiName,clazz) && StringUtility.isNotEmpty(jndiName)){
				final Context context = new InitialContext();
				return clazz.cast(context.lookup(jndiName));	
			}else{
				return null;
			}
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			return null;
		}
	}
}
